import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.time.LocalDate;
import java.util.*;
import java.util.concurrent.*;

public class TestMarmelab {
    @Test
    public void testSearch() {
        System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver");
        WebDriver driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.get("https://marmelab.com/react-admin-demo/#/login");
        By findTextLocatorUsername = By.xpath("//*[@id='username']");
        WebElement searchBoxUsername = driver.findElement(findTextLocatorUsername);
        searchBoxUsername.sendKeys("demo");
        searchBoxUsername.sendKeys(Keys.TAB);
        By findTextLocatorPassword = By.xpath("//*[@id='password']");
        WebElement searchBoxPassword = driver.findElement(findTextLocatorPassword);
        searchBoxPassword.sendKeys("demo");
        searchBoxPassword.sendKeys(Keys.ENTER);
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        By elementLocatorOrder = By.xpath("//*[text() = 'Orders']");
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        driver.findElement(elementLocatorOrder).click();
        By elementLocatorDelivered = By.xpath("//*[contains(text(),'delivered')]");
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        driver.findElement(elementLocatorDelivered).click();
        List<WebElement> elements = driver.findElements(By.xpath("//input[contains(@class, 'css-1m9pwf3')]"));
        for (int checkboxCount = 1; checkboxCount < elements.size(); checkboxCount++) {
            WebElement e = elements.get(checkboxCount);
            {
                if (checkboxCount == 4) break;
            }
            e.click();
        }
        boolean isButtonVisible = driver.findElement(By.xpath("//*[text() = '3 items selected']")).isDisplayed();
        if (isButtonVisible == true) {
            System.out.println("Появилось сообщение «3 items selected»");
        } else {
            throw new RuntimeException("Cообщение «3 items selected» не появилось");
        }
        By elementLocatorInvoices = By.xpath("//*[text() = 'Invoices']");
        driver.findElement(elementLocatorInvoices).click();
        By elementLocatorSince = By.xpath("//*[text() = 'Invoices']");
        driver.findElement(elementLocatorInvoices).click();
        LocalDate today = LocalDate.now();
        LocalDate today1 = today.minusYears(1);
        int year = today.getYear();
        By findTextPassedSince = By.xpath("//*[@id='date_gte']");
        WebElement searchBoxPassedSince = driver.findElement(findTextPassedSince);
        searchBoxPassedSince.sendKeys("01.01." + (year - 1));
        searchBoxPassedSince.sendKeys(Keys.TAB);
        By findTextPassedBefore = By.xpath("//*[@id='date_lte']");
        WebElement searchBoxPassedBefore = driver.findElement(findTextPassedBefore);
        searchBoxPassedBefore.sendKeys("01.08." + year);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        By openInvoices = By.xpath("//td[contains(@class, 'RaDatagrid-rowCell css-1o6fzn1')]");
        driver.findElement(openInvoices).click();
        String checkInvoices = driver.findElement
                (By.xpath("//p[contains(@class, 'MuiTypography-body1 css-9l3uo3')]")).getText();
        if (checkInvoices.equals("Korey Mohr")) {

            System.out.println("первый покупатель «Korey Mohr»");
        } else {
            System.out.println("первый покупатель не «Korey Mohr»");
        }
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.findElement(openInvoices).click();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        List<WebElement> randomInvoices = driver.findElements
                (By.xpath("//div[contains(@class, 'css-1j7qk7u')]"));
        int indexOfRandomElement = new Random().ints(10,
                randomInvoices.size() - 1).findFirst().getAsInt();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        randomInvoices.get(indexOfRandomElement).click();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        StringBuilder checkInvoiceCardContent = new StringBuilder(driver.findElement
                (By.xpath("//h6[contains(@class, 'css-d54f2v')]")).getText());
        checkInvoiceCardContent.delete(0, 8);
        String strCheckInvoiceCardContent = checkInvoiceCardContent.toString();
        String checkInvoice = driver.findElement(By.xpath
                        ("//td[contains(@class, 'css-1o6fzn1')]/*[text() = " + strCheckInvoiceCardContent + "]")).
                getText();
        if (strCheckInvoiceCardContent.equals(checkInvoice)) {
            System.out.println("Данные в поле Invoice совпадают!:-)");
        } else {
            System.out.println("Данные в поле Invoice не совпадают!!:(");
        }
        String checkDateCardContent = driver.findElement
                (By.xpath("//p[contains(@class, 'css-zjt4oc')]")).getText();
        String checkDate = driver.findElement
                (By.xpath("//td[contains(@class, 'css-1o6fzn1')]/*[text() = " + strCheckInvoiceCardContent + "]/following::*[2]")).getText();
        if (checkDateCardContent.equals(checkDate)) {
            System.out.println("Данные в поле Date совпадают!");
        } else {
            System.out.println("Данные в поле Date не совпадают!");
        }
        String checkCustomer = driver.findElement
                (By.xpath("//td[contains(@class, 'css-1o6fzn1')]/*[text() = " + strCheckInvoiceCardContent + "]/following::*[6]")).getText();
        String checkCustomerCardContent = driver.findElement
                (By.xpath("//p[contains(@class, 'css-9l3uo3')]")).getText();
        if (checkCustomerCardContent.startsWith(checkCustomer)) {
            System.out.println("Данные в поле Customer совпадают!");
        } else {
            System.out.println("Данные в поле Customer не совпадают!");
        }
        String orderCheckCard = driver.findElement
                (By.xpath("//p[contains(@class, 'css-1kggr9')]")).getText();
        String orderCheck = driver.findElement
                (By.xpath("//span[contains(@class, 'css-68o8xu')][text()='" + orderCheckCard + "']")).getText();
        if (orderCheckCard.equals(orderCheck)) {
            System.out.println("Данные в поле Order совпадают!");
        } else {
            System.out.println("Данные в поле Order не совпадают!");
        }
        int index1 = checkCustomer.indexOf(" ");
        String substr = checkCustomer.substring(index1 + 1);
        String substr2 = checkCustomer.substring(0, index1);
        By elementLocatorCustomer = By.xpath("//*[text() = 'Customers']/following::*[6]");
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        driver.findElement(elementLocatorCustomer).click();
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        By elementLocatorSearch = By.xpath("//input[contains(@class, 'MuiInputBase-inputAdornedEnd')]");
        driver.findElement(elementLocatorSearch).sendKeys(substr);
        By elementCustomerSearch = By.xpath("//*[text() = '" + substr2 + "']");
        driver.findElement(elementCustomerSearch).click();
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        By elementLocatorAddress = By.xpath("//textarea[contains(@class, 'css-j86opw')][@id='address']");
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        driver.findElement(elementLocatorAddress).click();
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        String oldAddress = driver.findElement
                (By.xpath("//textarea[contains(@class, 'css-j86opw')][@id='address']")).getText();
        driver.findElement(elementLocatorAddress).sendKeys((Keys.CONTROL + "a") + Keys.DELETE);
        driver.findElement(elementLocatorAddress).sendKeys("New_address!");
        By elementButtonSave = By.xpath("//button[contains(@aria-label, 'Save')]");
        driver.findElement(elementButtonSave).click();
        By elementInvoices = By.xpath("//a[contains(@class, 'css-1wuw629')][@href='#/invoices']");
        driver.findElement(elementInvoices).click();
        String checkCustomerCardNew = driver.findElement
                (By.xpath("//p[contains(@class, 'css-9l3uo3')]")).getText();
        int checkAddress = checkCustomerCardNew.indexOf("New_address!");
        if (checkAddress > -1) {
            System.out.println("Адрес поменялся!");
        } else {
            System.out.println("Адрес не изменился!");
        }
        driver.findElement(elementLocatorCustomer).click();
        driver.findElement(elementCustomerSearch).click();
        driver.findElement(elementLocatorAddress).sendKeys((Keys.CONTROL + "a") + Keys.DELETE);
        driver.findElement(elementLocatorAddress).sendKeys(oldAddress);
        driver.findElement(elementButtonSave).click();
        driver.quit();

    }


}

